package smartclass.adapter;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import smartclass.bussiness.DocumentBussiness;
import smartclass.data.FinalValues;
import smartclass.entity.DocumentInfo;
import smartclass.entity.TopicInfo;
import smartclass.fragment.TeacherFolderFragment;
import smartclass.utils.Helper;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.example.R;

public class ExpandListDocumentAdapter extends BaseExpandableListAdapter {

	private Context context;
	private Map<TopicInfo, List<DocumentInfo>> documentCollections;
	private List<TopicInfo> topics;
	private String name;
	private ExpandableListView expandListDocument;
	private DocumentBussiness documentBussiness;
	private View viewChild;

	public ExpandListDocumentAdapter(Context context, List<TopicInfo> topics,
			Map<TopicInfo, List<DocumentInfo>> documentCollections,
			ExpandableListView expandListDocument) {
		this.context = context;
		this.documentCollections = documentCollections;
		this.topics = topics;
		this.expandListDocument = expandListDocument;
		documentBussiness = DocumentBussiness.getInstance();
		viewChild = (LayoutInflater.from(context).inflate(R.layout.child_item,
				expandListDocument, false));
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return documentCollections.get(topics.get(groupPosition)).get(
				childPosition);
	}

	@Override
	public long getChildId(int groupPostion, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, final ViewGroup parent) {
		// TODO Auto-generated method stub
		final DocumentInfo document = (DocumentInfo) getChild(groupPosition,
				childPosition);
		final TopicInfo topic = (TopicInfo) getGroup(groupPosition);
		final LayoutInflater inflater = LayoutInflater.from(context);
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.child_item, parent, false);
		}
		TextView item = (TextView) convertView.findViewById(R.id.laptop);
		ImageView delete = (ImageView) convertView.findViewById(R.id.delete);
		delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				View v = (LayoutInflater.from(context)).inflate(
						R.layout.information_dialog, null);
				builder.setTitle("Document Information");
				builder.setIcon(R.drawable.medical_invoice_information);
				builder.setView(v);
				TextView tvName = (TextView) v
						.findViewById(R.id.tvInformationName);
				TextView tvDateCreate = (TextView) v
						.findViewById(R.id.tvInformationDateCreat);
				TextView tvPath = (TextView) v
						.findViewById(R.id.tvInformationPath);
				TextView tvDescription = (TextView) v
						.findViewById(R.id.tvInformationDescription);
				tvName.setText(document.getName());
				tvDateCreate.setText((new Date(Long.parseLong(document
						.getDateCreate()))).toString());
				tvPath.setText(document.getPath());
				tvDescription.setText(document.getDocumentInfo());
				builder.setPositiveButton("Delete",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								List<DocumentInfo> child = documentCollections
										.get(topics.get(groupPosition));
								documentBussiness.deleteDocument(document);
								TeacherFolderFragment.totalHeightListDocument = Helper
										.removeItemgetSize(
												expandListDocument,
												TeacherFolderFragment.totalHeightListDocument,
												inflater.inflate(
														R.layout.child_item,
														parent, false));
								updateListChild(child, topic);
								notifyDataSetChanged();
							}
						});
				builder.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
				builder.setNeutralButton("Share",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

							}
						});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
			}
		});

		item.setText(document.getName());
		viewChild = convertView;
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		if (documentCollections.get(topics.get(groupPosition)) != null) {

			return documentCollections.get(topics.get(groupPosition)).size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return topics.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return topics.size();
	}

	@Override
	public long getGroupId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final TopicInfo topic = (TopicInfo) getGroup(groupPosition);

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.group_item, null);
		}
		ImageView imgAdd = (ImageView) convertView.findViewById(R.id.add);
		imgAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				FinalValues.displayNotification(context, "Add");
				List<DocumentInfo> child = documentCollections.get(topics
						.get(groupPosition));
				if (child == null) {
					FinalValues.displayNotification(context, "Child null");
					child = new ArrayList<DocumentInfo>();
				}

				creatDialog(groupPosition, topic, child);
			}
		});
		TextView item = (TextView) convertView.findViewById(R.id.laptop);
		item.setTypeface(null, Typeface.BOLD);
		item.setText(topic.getName());
		return convertView;

	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

	public void creatDialog(final int groupPosition, final TopicInfo topic,
			final List<DocumentInfo> child) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(topic.getName());
		LayoutInflater inflater = LayoutInflater.from(context);
		builder.setIcon(R.drawable.gnome_mime_application_x_class_file);
		View v = inflater.inflate(R.layout.add_new_item_dialog, null);
		builder.setCancelable(false);
		builder.setView(v);
		final EditText edtName = (EditText) v.findViewById(R.id.nameNewItem);
		builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				name = edtName.getText().toString();
				// Add new Bai giang
				DocumentInfo document = new DocumentInfo();

				if (name.isEmpty()
						|| !documentBussiness.checkNameAvailable(name)) {
					FinalValues.displayNotification(context,
							"Name is not available");
					return;
				}
				document.setName(name);
				document.setIdTopic(topic.getId());
				document.setDateCreate(String.valueOf(System
						.currentTimeMillis()));
				document.setPath(FinalValues.FILE_PATH_DOCUMENT + name + ".spd");
				documentBussiness.addDocument(document);
				if (documentBussiness.getCheckExpandValues(groupPosition)) {
					TeacherFolderFragment.totalHeightListDocument = Helper
							.addNewItemgetSize(
									expandListDocument,
									TeacherFolderFragment.totalHeightListDocument,
									viewChild);
				}
				updateListChild(child, topic);

			}
		});
		builder.setNeutralButton("Delete All Document",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Log.e("Size",
								documentBussiness.getAllDocument(topic.getId())
										.size() + " ");
						for (int size = 0; size < documentBussiness
								.getAllDocument(topic.getId()).size(); size++) {
							TeacherFolderFragment.totalHeightListDocument = Helper
									.removeItemgetSize(
											expandListDocument,
											TeacherFolderFragment.totalHeightListDocument,
											viewChild);
						}
						documentBussiness.deleteArrayDocument(topic.getId());
						updateListChild(child, topic);
					}
				});
		builder.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

	private void updateListChild(List<DocumentInfo> child, TopicInfo topic) {
		child = new ArrayList<DocumentInfo>();
		child = documentBussiness.getAllDocument(topic.getId());
		Log.e("ID Topic", topic.getId() + " ");
		if (child.size() == 0) {
			Log.e("Child", "Child ko co phan tu nao");
		}
		for (DocumentInfo doc : child) {
			Log.e("Document", doc.getName());
		}
		documentCollections.put(topic, child);
		notifyDataSetChanged();
	}
}
